package com.base.controller;


import cn.dev33.satoken.util.SaResult;
import com.base.dto.GuestDto;
import com.base.entity.CheckInRecord;
import com.base.entity.FeeRecord;
import com.base.entity.Guest;
import com.base.entity.Room;
import com.base.service.ICheckInRecordService;
import com.base.service.IFeeRecordService;
import com.base.service.IGuestService;
import com.base.service.IRoomService;
import com.base.vo.CheckInRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-11-30
 */
@Controller
@RequestMapping("/check-in-record")
public class CheckInRecordController {

    @Autowired
    private IGuestService guestService;

    @Autowired
    private ICheckInRecordService checkInRecordService;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IFeeRecordService feeRecordService;

//  入住登记
    @RequestMapping("/checkIn")
    @ResponseBody
    public SaResult checkIn(GuestDto guestDto){
        Guest guest = new Guest();
        BeanUtils.copyProperties(guestDto,guest);
//        客户登记
        guestService.save(guest);
//        入住登记
        CheckInRecord record = new CheckInRecord();
        record.setGuestId(guest.getId());
        record.setInTime(LocalDateTime.now());
        record.setRoomId(guestDto.getRoomId());
        checkInRecordService.save(record);

//        更新客房状态
        Room room = roomService.getById(guestDto.getRoomId());
        room.setIsFree(0);
        roomService.updateById(room);

        //      添加消费记录
        FeeRecord feeRecord = new FeeRecord();
        feeRecord.setCheckInId(record.getId());
        feeRecord.setAmount(room.getPrice());
        feeRecord.setGuestId(guest.getId());
        feeRecord.setCreateAt(LocalDateTime.now());
        feeRecordService.save(feeRecord);
        return SaResult.ok();
    }

//   入住记录
    @GetMapping("/list")
    public String list(Model model){
        List<CheckInRecord> records = checkInRecordService.list();
//        客户信息
        ArrayList<CheckInRecordVo> checkInRecordVos = new ArrayList<>();
        for (CheckInRecord record : records) {
            CheckInRecordVo checkInRecordVo = new CheckInRecordVo();
            BeanUtils.copyProperties(record,checkInRecordVo);
            Guest guest = guestService.getById(record.getGuestId());
            checkInRecordVo.setGuest(guest);
            Room room = roomService.getById(record.getRoomId());
            checkInRecordVo.setRoom(room);
            checkInRecordVos.add(checkInRecordVo);
        }
        model.addAttribute("records",checkInRecordVos);
        return "check_record";
    }

    //   入住记录
    @GetMapping("/search")
    public String search(Model model,@ModelAttribute Guest g){
        List<CheckInRecord> records = checkInRecordService.search(g);
//        客户信息
        ArrayList<CheckInRecordVo> checkInRecordVos = new ArrayList<>();
        for (CheckInRecord record : records) {
            CheckInRecordVo checkInRecordVo = new CheckInRecordVo();
            BeanUtils.copyProperties(record,checkInRecordVo);
            Guest guest = guestService.getById(record.getGuestId());
            checkInRecordVo.setGuest(guest);
            Room room = roomService.getById(record.getRoomId());
            checkInRecordVo.setRoom(room);
            checkInRecordVos.add(checkInRecordVo);
        }
        model.addAttribute("records",checkInRecordVos);
        return "check_record";
    }

    @RequestMapping("/checkOut/{check_id}")
    @ResponseBody
    public SaResult checkOut(@PathVariable Integer check_id){
//        更新退房时间
        CheckInRecord record = checkInRecordService.getById(check_id);
        record.setOutTime(LocalDateTime.now());
        checkInRecordService.updateById(record);
//        更新客房状态
        Room room = roomService.getById(record.getRoomId());
        room.setIsFree(1);
        roomService.updateById(room);
        return SaResult.ok();
    }



}
